/*
 * ServerSocketForDevice.h
 *
 *  Created on: 2013-3-14
 *      Author: song
 */

#ifndef SVRSOCKET4DEV_H_
#define SVRSOCKET4DEV_H_

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pthread.h>

#include <netinet/in.h>
#include <arpa/inet.h>

#include <net/if.h>
#include <netinet/tcp.h>

#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <list>
#include "debug.h"
#include "iostream"
#include "types.h"

#define DEVPORT  20131     /*服务器监听命令端口号 */
#define DEVNUM		1000


#define DEVCMD_READ_TEMP_SENSOR 0x1001
#define DEVCMD_READ_RELAY 		0x1002
#define DEVCMD_SET_RELAY 		0x1003
#define DEVCMD_OPEN_AC			0x1004//开空调
#define DEVCMD_CLOSE_AC			0x1005//关空调

using namespace std;




typedef struct
{
int fd;
//DeviceID_t DeviceID;

}Fd2DeviceID_t;

class SvrSocket4Dev
{
public:
	 //打开服务端口,等待连接
	SvrSocket4Dev();
	~SvrSocket4Dev(){};
	int  StartSvr();   //启动命令端口服务
	void StopSvr();    //停止命令端口服务

	void CloseFd(int fd);

	void ParseData(int fd);
	bool CheckCmd(Cmd_t &cmd);

	int SendCmd(int fd,Hu32Value_t &send);
	int AnswerErr(int fd,uint16 cmd,int err);

	int RecCmd(int fd,Cmd_t &cmd);
	int RecCmd(int fd,DevInfo_t &devInfo);
	int RecCmd(int fd,u32Value_t &value);

	bool IsCanBind(int devId);
	bool IsOnline(int devId);
	int  GetFd(int devId);

	int  GetDevId(int fd);

	void BindDevId(int fd,int devId);
	void UnBindDevId(int fd);

	void DisMap(int len);

	int  m_ListenSock;  //侦听端口
	int  m_ClientSock;  //客户端口
	char m_sClientIP[20];  //用户软件IP

	fd_set m_readfds;
	int m_devId[1024];

private:

	pthread_t m_pthServer;



};

#endif /* DEVSVRSOCKET_H_ */

